package cn.yht.wang.controller;

import cn.yht.wang.bean.SysRole;
import cn.yht.wang.bean.SysUser;
import cn.yht.wang.common.ApiResult;
import cn.yht.wang.mapper.UserMapper;
import cn.yht.wang.utils.JWTUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 *
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/user")
public class UserController {


    @Autowired
    UserMapper userMapper;
//  @Autowired
//  JdbcTemplate jdbcTemplate;

  Logger logger = LoggerFactory.getLogger(UserController.class);

  @ApiOperation(value = "新增用户", notes="新增注册")
  @PostMapping(value="/")
  public ApiResult<String> create(@Valid @RequestBody SysUser user){
    logger.info("create:::{}", user.toString());
    int result = userMapper.insertUser(user);
    return new ApiResult<>("0000", "新增成功.",""+result);
  }

  @ApiOperation(value = "修改用户", notes="修改用户")
  @PutMapping(value="/")
  public ApiResult<String> update(@Valid @RequestBody SysUser user){
    logger.info("create:::{}", user.toString());
    int result = userMapper.updateUser(user);
    return new ApiResult<>("0000", "修改成功.",""+result);
  }

  @ApiOperation(value = "删除用户", notes="删除用户")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "path", dataType = "java.lang.Integer")
  })
  @DeleteMapping(value="/{userId}")
  public ApiResult<String> deleteByUserId(@PathVariable("userId") Long userId){
    logger.info("delete:::{}", userId);
    int result= userMapper.deleteUserById(userId);
    return new ApiResult<>("0000", "删除成功.",""+result);
  }

  @ApiOperation(value = "查询用户", notes="查询用户")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "userId", value = "用户标识", required = true, paramType = "path", dataType = "java.lang.Integer")
  })
  @GetMapping(value="/{userId}")
  public ApiResult<SysUser> getByUserId(@PathVariable("userId") Long userId){
    logger.info("query:::{}", userId);
    SysUser user = userMapper.queryUserByIdentity(userId);
    return new ApiResult<>("0000", "查询成功.", user);
  }

  @GetMapping(value="/getUserList")
  public ApiResult<List<SysUser>> getByUserList(){
    List<SysUser> result = userMapper.queryUserList();
    return new ApiResult<>("0000", "查询成功.", result);
  }

  @GetMapping(value="/getByRolesListByUserId/{userId}")
  public ApiResult<List<SysRole>> getByRolesListByUserId(@PathVariable("userId") Long userId){
    List<SysRole> result = userMapper.selectRolesByUserId(userId);
    return new ApiResult<>("0000", "查询成功.", result);
  }

  @GetMapping(value="/selectAllUserAndRoles")
  public ApiResult<List<SysUser>> getByRolesListByUserId(){
    List<SysUser> result = userMapper.queryUserList();
    return new ApiResult<>("0000", "查询成功.", result);
  }

  @GetMapping(value="/selectDeluxeAllUserAndRoles")
  public ApiResult<List<SysUser>> getDeluxeSelectAllUserAndRoles(){
    List<SysUser> result = userMapper.deluxeSelectAllUserAndRoles();

    return new ApiResult<>("0000", "查询成功.", result);
  }

  @GetMapping(value="/selectMapList")
  public ApiResult<List<Map<String,Object>>> selectMapList(){
    List<Map<String,Object>> result = userMapper.selectMapList();
    return new ApiResult<>("0000", "查询成功.", result);
  }



}